(function(window, $api) {
    var m = {};
    m.init = function(callback) {
        var ajpush = api.require('ajpush');
        ajpush.init(function(ret) {
            if (typeof callback == 'function') {
                callback(ret)
            }
        })
    }
    m.Listener = function(callback) {
            var systemType = api.systemType
            var ajpush = api.require('ajpush');
            //   ajpush.setListener(
            //     function(ret) {
            //         alert(JSON.stringify(ret))
            //     }
            // );
            ajpush.removeListener();
            if (systemType == 'ios') { //ios
                api.addEventListener({
                    name: 'noticeclicked'
                }, function(ret, err) {
                    if (ret && ret.value) {
                        // vm.isread = true;
                        var articleid = null;
                        if (ret.value.extra.articleid) {
                            articleid = ret.value.extra.articleid;
                        }

                        if (typeof callback == 'function') {
                            callback(ret, ret.value.extra.articleid)
                        }
                    }
                })
                ajpush.setBadge({
                    badge: 0
                });
            } else if (systemType == 'android') { //安卓
                m.init()
                api.addEventListener({
                    name: 'appintent'
                }, function(ret, err) {
                    if (ret && ret.appParam.ajpush) {
                        if (typeof callback == 'function') {
                            callback(ret, ret.appParam.ajpush.extra.articleid)
                        }
                    }
                })
            }
        }
        //登录的时候调用，res就是用户id
    m.bingdalias = function(res) {
        var ajpush = api.require('ajpush');
        var param = {
            alias: res
        };
        ajpush.bindAliasAndTags(param, function(ret) {
            var statusCode = ret.statusCode;
        });
    }

    window.jiguang = m;
})(window, $api)
